package logic

import (
	"class/models"
	"context"
	"fmt"
	"gorm.io/gorm"

	"class/classrpc/classrpc"
	"class/classrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *UpdateLogic) Update(in *classrpc.UpdateRequest) (*classrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User
	models.DB.Debug().Where("account = ?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	user = models.User{
		Model:    gorm.Model{ID: uint(in.UserId)},
		Account:  in.Account,
		Password: in.Password,
		Name:     in.Name,
	}

	return &classrpc.UpdateResponse{}, nil
}
